//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080/'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios

//======================axios的前置拦截器【拦截调用后端的请求====================//
axios.interceptors.request.use(res=>{
    let token = localStorage.getItem("token");
    if(token){
        res.headers["token"] = token;
    }
    return res;
},error => {
    Promise.reject(error)
})
//======================axios的前置拦截器【拦截调用后端的请求】====================//
//======================axios的后置拦截器【处理后台登录拦截的结果】====================//
axios.interceptors.response.use(res => {
    //后端响应的是没有登录的信息
    if (false === res.data.success && "noLogin" === res.data.msg) {
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href="/login.html";
    }
    return res;
},error => {
    Promise.reject(error)
})
//======================axios的后置拦截器【处理后台登录拦截的结果】====================//
//静态资源拦截器：拦截不发送axios请求的资源
//核心思路：如果你访问的不是login.html 也不是register.html。就要判断当前用户是否登录过
let url = location.href;//http://localhost/login.html - 获取当前网页路径
let path = url.substring(url.lastIndexOf("/"));
if(path != "/login.html" && path != "/register.html" &&
    url.indexOf("/callback.html") == -1 && url.indexOf("/binder.html") == -1){
    let logininfo = localStorage.getItem("logininfo");
    if(!logininfo){
        location.href="/login.html";
    }
}

//将url地址栏后的参数解析成一个json对象
function parseUrlParams2Obj(url){ // xxxx?code=111&key=222 => {"code":111,"key":222}
    var obj = {};
    //截取？后面的数据 = 参数们
    let params = url.substring(url.indexOf("?")+1);// code=111&key=222
    //将参数分隔
    let paramsArr = params.split("&"); // [code=111,key=222]
    for (var i = 0;i<paramsArr.length;i++){
        //每一个参数
        let param = paramsArr[i]; //code=111  or key=222
        let paramArr = param.split("=");
        //对象的[属性名] = 属性值
        obj[paramArr[0]] = paramArr[1];
    }
    return obj;
}

